package ru.osu.acm.testing.beans;

import ru.osu.acm.common.entities.Group;
import ru.osu.acm.common.entities.Person;
import ru.osu.acm.common.config.Config;
import ru.osu.acm.testing.Utils;

import javax.annotation.Resource;
import javax.faces.model.SelectItem;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * User: Integra
 * Date: 10.07.2009
 * Time: 17:55:34
 */
public class ShowPersonBean {
    @Resource(name = Config.jndi)
    private DataSource source;

    private PersonBean[] persons;

    public String Get() {

        ArrayList<PersonBean> pers = new ArrayList<PersonBean>();
        int cnt = 0;
        try {
            Connection conn = source.getConnection();
            try {
                PreparedStatement statement = conn.prepareStatement("select * from person order by id");
                ResultSet resultSet = statement.executeQuery();
                while (resultSet.next()) {
                    cnt++;
                    PreparedStatement statementGroup = conn.prepareStatement("select persongroup from persongroupmember where person = ? ");
                    statementGroup.setInt(1, resultSet.getInt("id"));
                    ResultSet resGroup = statementGroup.executeQuery();
                    String group = "";
                    boolean first = true;
                    while (resGroup.next()) {
                        PreparedStatement statementName = conn.prepareStatement("select * from persongroup where id = ? ");
                        statementName.setInt(1, resGroup.getInt("persongroup"));
                        ResultSet resName = statementName.executeQuery();
                        if (resName.next()) {
                            if (first) {
                                group += resName.getString("groupname");
                                first = false;
                            } else
                                group += ", " + resName.getString("groupname");
                        }

                        resName.close();
                        statementName.close();
                    }
                    pers.add(new PersonBean(
                            resultSet.getInt("id"),
                            resultSet.getString("login"),
                            resultSet.getString("pass"),
                            group,
                            resultSet.getString("firstname"),
                            resultSet.getString("secondname"),
                            false,
                            false
                    ));
                    resGroup.close();
                    statementGroup.close();
                }
                resultSet.close();
                statement.close();
            }
            finally {
                conn.close();
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        persons = new PersonBean[cnt];
        int count = 0;
        for (PersonBean i : pers) {
            persons[count++] = i;
        }
        return "null";
    }

    public PersonBean[] getPersons() {
        Get();
        return persons;
    }

    public void setPersons(PersonBean[] persons) {
        this.persons = persons;

    }


}